package com.tbit.main.controller;

import com.tbit.main.common.JsonResult;
import com.tbit.main.constant.BatteryConstant;
import com.tbit.main.exception.BaseException;
import com.tbit.main.pojo.AccountUser;
import com.tbit.main.pojo.Battery;
import com.tbit.main.pojo.BatteryGroup;
import com.tbit.main.pojo.BatteryGroupBound;
import com.tbit.main.service.*;
import com.tbit.main.util.Assert;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.LinkedList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 电池分组
 *
 * @author LMY
 * @create 2022-05-03 16:04
 */
@RestController
@RequestMapping("/batteryGroupController")
public class BatteryGroupController {

    @Autowired
    private BatteryGroupService batteryGroupService;
    @Autowired
    private TokenService tokenService;
    @Autowired
    private BatteryGroupBoundService batteryGroupBoundService;
    @Autowired
    private DataService dataService;
    @Autowired
    private BatteryLogService batteryLogService;
    @Autowired
    private BatteryService batteryService;

    /**
     * 添加分组
     */
    @RequestMapping("/add")
    public JsonResult add(String token, Integer accountId) {

        Assert.notNull(accountId, "AccountUser.agentId.notNull");

        AccountUser accountUser = tokenService.getAccount(token);
        BatteryGroup batteryGroup = new BatteryGroup();
        batteryGroup.setAccountUserId(accountUser.getAccountUserId());
        batteryGroup.setAccountId(accountId);
        batteryGroup.setType(BatteryConstant.battery_Group_CK);
        batteryGroupService.add(batteryGroup);
        return JsonResult.succ(batteryGroup.getId());
    }

    /**
     * 删除电池分组（出库的电池再入库）取消待出库
     */
    @RequestMapping("/del")
    public JsonResult del(String token, Integer id) {

        Assert.notNull(id, "Machine.move.id.notNull");

        AccountUser accountUser = tokenService.getAccount(token);

        List<BatteryGroupBound> batteryList = batteryGroupBoundService.getId(id);

        batteryGroupService.del(id);

        if (batteryList != null && batteryList.size() > 0) {
            List<String> batteryNOs = batteryList.stream().map(BatteryGroupBound::getBatteryNO).collect(Collectors.toList());

            batteryService.updateState(batteryNOs, accountUser.getAccountUserId(), BatteryConstant.Battery_State_KGZ);

            batteryLogService.inserts(batteryNOs, accountUser.getAccountUserId(), BatteryConstant.Battery_Type_SCDCK);
        }

        return JsonResult.succ();
    }

    /**
     * 修改电池分组(出库、取消出库)取消待出库
     */
    @RequestMapping("/update")
    public JsonResult update(String token, Integer id, String batteryNO, Integer type) {

        Assert.notNull(id, "Machine.move.id.notNull");
        Assert.notNull(batteryNO, "Battery.batteryNO.notNull");
        Assert.notNull(type, "Battery.type.notNull");

        AccountUser accountUser = tokenService.getAccount(token);

        batteryService.auth(accountUser.getAgents(), dataService.getBatteryId(batteryNO));

        /**添加*/
        if (type == 1) {

            Battery battery = batteryService.getByNO(batteryNO);
            /**判断出库*/
            if (!battery.getState().equals(BatteryConstant.Battery_State_KGZ) && !battery.getState().equals(BatteryConstant.Battery_State_DCK)) {
                throw new BaseException("Battery.batteryNO.notCK");
            }

            batteryGroupBoundService.addBatteryNO(id, batteryNO, accountUser.getAccountUserId());

            batteryService.updateIdState(batteryNO, null, accountUser.getAccountUserId(), BatteryConstant.Battery_State_DCK, null);

            batteryLogService.insert(batteryNO, accountUser.getAccountUserId(), BatteryConstant.Battery_Type_DCK, null);
        } else {

            Battery battery = batteryService.getByNO(batteryNO);
            /**判断入库*/
            if (!battery.getState().equals(BatteryConstant.Battery_State_DCK)) {
                throw new BaseException("Battery.batteryNO.notCK");
            }

            /**删除*/
            batteryGroupBoundService.delBatteryNO(batteryNO);

            batteryService.updateIdState(batteryNO, null, accountUser.getAccountUserId(), BatteryConstant.Battery_State_KGZ, null);

            batteryLogService.insert(batteryNO, accountUser.getAccountUserId(), BatteryConstant.Battery_Type_SCDCK, null);
        }
        return JsonResult.succ();
    }

    /**
     * 根据id，电池编号查询分组
     */
    @RequestMapping("/get")
    public JsonResult get(String token, Integer id, String batteryNO) {

        List<Battery> batteries = new LinkedList<>();

        AccountUser accountUser = tokenService.getAccount(token);

        if (id != null) {
            batteries = batteryGroupBoundService.getBatteryById(id);
        } else {
            batteryService.auth(accountUser.getAgents(), dataService.getBatteryId(batteryNO));

            BatteryGroupBound batteryGroupBound = batteryGroupBoundService.getNO(batteryNO);

            if (batteryGroupBound == null) {
                throw new BaseException("Group.is.null");
            }

            batteries = batteryGroupBoundService.getBatteryNO(batteryNO);

        }
        return JsonResult.succ(batteries);

    }

    /**
     * 查询所有分组
     */
    @RequestMapping("/getAccountId")
    public JsonResult getAccountId(String token, Integer accountId) {

        Assert.notNull(accountId, "AccountUser.agentId.notNull");

        List<BatteryGroup> batteryGroupBounds = batteryGroupBoundService.getGroupAccountId(accountId);

        return JsonResult.succ(batteryGroupBounds);
    }

    /**
     * 查询未分组车辆
     */
    @RequestMapping("/getNotGroup")
    public JsonResult getNotGroup(String token, Integer accountId) {

        Assert.notNull(accountId, "AccountUser.agentId.notNull");

        List<Battery> batteryList = batteryService.getNotGroup(accountId);

        return JsonResult.succ(batteryList);
    }


    /**
     * 生成移交电池分组
     */
    @RequestMapping("/addRemove")
    public JsonResult addRemove(String token,
                                @RequestParam(value = "batteryNOs", required = false) List<String> batteryNOs) {

        Assert.notNull(batteryNOs, "Battery.batteryNO.notNull");

        AccountUser accountUser = tokenService.getAccount(token);

        batteryService.auth(accountUser.getAgents(), dataService.getBatteryId(batteryNOs.get(0)));

        List<BatteryGroupBound> batteryGroups = batteryGroupBoundService.getByBatteryNOs(batteryNOs);
        if (batteryGroups != null && batteryGroups.size() > 0) {
            batteryGroupService.del(batteryGroups.get(0).getId());
        }

        BatteryGroup batteryGroup = new BatteryGroup();
        batteryGroup.setAccountId(dataService.getBatteryAccountId(batteryNOs.get(0)));
        batteryGroup.setAccountUserId(accountUser.getAccountUserId());
        batteryGroup.setType(BatteryConstant.battery_Group_YJ);
        batteryGroupService.add(batteryGroup);

        batteryGroupBoundService.addBatteryNOs(batteryGroup.getId(), batteryNOs);

        return JsonResult.succ();
    }

    /**
     * 移交电池（电池所在的分组移交）
     */
    @RequestMapping("/remove")
    public JsonResult remove(String token, String batteryNO) {

        Assert.notNull(batteryNO, "Battery.batteryNO.notNull");

        AccountUser accountUser = tokenService.getAccount(token);

        batteryService.auth(accountUser.getAgents(), dataService.getBatteryId(batteryNO));

        List<BatteryGroupBound> batteryGroupBound = batteryGroupBoundService.getNOs(batteryNO);

        if (batteryGroupBound != null && batteryGroupBound.size() > 0) {
            batteryService.updateState(batteryGroupBound.stream().map(BatteryGroupBound::getBatteryNO).collect(Collectors.toList()), accountUser.getAccountUserId(), null);
            batteryGroupService.del(batteryGroupBound.get(0).getId());
        } else {
            batteryService.updateIdState(batteryNO, null, accountUser.getAccountUserId(), null, null);
        }

        return JsonResult.succ();
    }


}
